from langchain_core.prompts import ChatPromptTemplate, ChatMessagePromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from pydantic import BaseModel, Field, SecretStr
from dotenv import load_dotenv
import os


"""大模型及提示词实例, 及tool工具抽象"""
# 加载.env文件中的环境变量
load_dotenv()

llm = ChatOpenAI(
    model="qwen-flash",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key=SecretStr(os.getenv("DASHSCOPE_API_KEY")),
    streaming=True
)

# 系统消息模板
system_message_template = ChatMessagePromptTemplate.from_template(
    template="你是一位{role}专家, 擅长解决{domain}领域的问题",
    role="system"
)
# 用户消息模板
human_message_template = ChatMessagePromptTemplate.from_template(
    template="用户的问题:{question}",
    role="user"
)
# 创建提示词模板
chat_prompt_template = ChatPromptTemplate.from_messages([
    system_message_template,
    human_message_template
])


# 自定义工具
class FetchWeatherInputArgs(BaseModel):
    city: str = Field(description="城市名称")


# 1.开发自定义工具, 并注册tool
@tool(
    description="获取某个城市的真实天气",
    args_schema=FetchWeatherInputArgs
)
def fetch_weather(city: str) -> str:
    """获取某个城市的真实天气"""
    # 模拟调用天气API逻辑...
    weather_data = {
        '北京': '多云',
        '深圳': '晴朗'
    }
    if city not in weather_data.keys():
        return '天气多变'
    return weather_data[city]


def create_calc_tools():
    return [fetch_weather]


calc_tools = create_calc_tools()
